﻿namespace ScrumLite.WebUx.Controllers
{
    using System.Web.Mvc;
    using ScrumLite.Services;
    using ScrumLite.Services.Contracts;
    using ScrumLite.WebUx.Models;
    using ScrumLite.Gateways;
    using System.Collections.ObjectModel;
    using System.Security.Permissions;

    public class PeopleController : ContextBaseController
    {
        IProfileService profile;
        IPeopleService people;

        public PeopleController()
            : this(new ProfileServiceClient(), new PeopleServiceClient())
        {
        }

        public PeopleController(IProfileService profile, IPeopleService people)
        {
            this.profile = profile;
            this.people = people;
        }

        [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)]
        public void List()
        {
            PeopleListViewData data = new PeopleListViewData(profile.GetPeople());

            RenderView("List", data);
        }

        public void Education(string personId)
        {
            PersonEducationViewData data;

            Education[] education = people.GetPersonEducation(personId);
            if (education != null)
            {
                data = new PersonEducationViewData(new Collection<Education>(education));
            }
            else
            {
                data = new PersonEducationViewData();
            }

            data.Person = new Person { Id = personId, Name = profile.GetFullName(personId) };
            
            RenderView("Education", data);
        }

        public void Abilities(string personId)
        {
            PersonAbilitiesViewData data;

            Ability[] abilities = people.GetPersonAbilities(personId);

            if (abilities != null)
            {
                data = new PersonAbilitiesViewData(new Collection<Ability>(abilities));
            }
            else
            {
                data = new PersonAbilitiesViewData();
            }
            data.Person = new Person { Id = personId, Name = profile.GetFullName(personId) };

            RenderView("Abilities", data);
        }
    }
}
